package cn.doitedu.sql;

import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class _11_RegularJoin {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.enableCheckpointing(1000, CheckpointingMode.EXACTLY_ONCE);
        env.getCheckpointConfig().setCheckpointStorage("file:///d:/ckpt");

        env.setParallelism(1);

        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);
        tenv.getConfig().set("table.exec.source.idle-timeout","1s");
        tenv.getConfig().set("table.exec.state.ttl","1 hour");


        // {"uid":1,"event_id":"face_recg","action_time":10000}
        tenv.executeSql(
                " create table face_log_kfk(                                      "+
                        "   uid int,                                            "+
                        " 	event_id string,                                    "+
                        " 	action_time bigint,                                 "+
                        " 	pt as proctime(),                                   "+
                        " 	rt as to_timestamp_ltz(action_time,3),              "+
                        " 	watermark for rt as rt                              "+
                        "  ) with (                                             "+
                        "    'connector' = 'kafka',                             "+
                        "    'topic' = 'face-log',                            "+
                        "    'properties.bootstrap.servers' = 'doitedu:9092',   "+
                        "    'properties.group.id' = 'g003',                    "+
                        "    'scan.startup.mode' = 'latest-offset',             "+
                        "    'value.format' = 'json',                           "+
                        "    'value.fields-include' = 'EXCEPT_KEY'              "+
                        " )                                                     "
        );

        // {"uid":1,"face_status":1,"ts":11000}
        tenv.executeSql(
                " create table face_res_kfk(                                    "+
                        "   uid int,                                            "+
                        " 	face_status int,                                    "+
                        " 	ts bigint,                                          "+
                        " 	pt as proctime(),                                   "+
                        " 	rt as to_timestamp_ltz(ts,3),                       "+
                        " 	watermark for rt as rt                              "+
                        "  ) with (                                             "+
                        "    'connector' = 'kafka',                             "+
                        "    'topic' = 'face-res',                            "+
                        "    'properties.bootstrap.servers' = 'doitedu:9092',   "+
                        "    'properties.group.id' = 'g003',                    "+
                        "    'scan.startup.mode' = 'latest-offset',             "+
                        "    'value.format' = 'json',                           "+
                        "    'value.fields-include' = 'EXCEPT_KEY'              "+
                        " )                                                     "
        );

        // 按相同uid关联
        tenv.executeSql(
                        " select                   "+
                        "   a.uid,                 "+
                        " 	a.event_id,            "+
                        " 	a.action_time,         "+
                        " 	b.face_status,         "+
                        " 	b.ts as status_time    "+
                        " from face_log_kfk a      "+
                        " left join face_res_kfk b "+
                        " on a.uid = b.uid         "
        ).print();



    }

}
